package com.example.zhiyeaiagent.tools;


import cn.hutool.core.io.FileUtil;
import com.example.zhiyeaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * 文件操作工具类（提供文件读写功能）
 */
public class FileOperationTool {
    private static final String FILE_DIR= FileConstant.FILE_SAVE_DIR+"/file";

    /**
     * 读取文件
     * @param fileName
     * @return
     */
    @Tool(description = "read content from a file")
    public String readFile(@ToolParam(description = "Name of a file to read") String fileName) {
        String filePath = FILE_DIR + "/" +fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "Error reading file :"+e.getMessage();
        }
    }

    /**
     * 写入文件
     * @param fileName
     * @param content
     * @return
     */
    @Tool(description = "write content to a file")
    public String writeFile(@ToolParam(description = "Name of a file to write") String fileName,
                            @ToolParam(description = "Content to write to the file") String content) {
        String filePath = FILE_DIR + "/" +fileName;
        try{
            //创建目录  如果已存在不会创建
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content,filePath);
            return "File written successfully to :"+ filePath;
        }catch (Exception e){
            return "Error writing file :"+e.getMessage();
        }
    }
}
